<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\App;

class PersonResource extends JsonResource
{
  protected $formats;

  public function toArray($request)
  {
    $this->formats = config('coreapi.' . App::getLocale());

    return [
      'id' => $this->id,
      'name' => $this->name,
      'surname' => $this->surname,
      'sex' => $this->sex,
      'birthdate' => isset($this->birthdate) ? localizedFormat($this->birthdate, 'datetime') : null,
      'birthplace' => $this->birthplace,
      'fiscalcode' => $this->fiscalcode,
      'active' => $this->active,
      'created_at' => localizedFormat($this->created_at, 'datetime'),
      'updated_at' => localizedFormat($this->updated_at, 'datetime'),
      'user' => new UserResource($this->whenLoaded('user')),
    ];
  }
}
